﻿using apointment.Jobs;
using apointment.Util;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace apointment.Controllers
{
    public class TaskSchedulingController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [Route("TaskScheduling")]
        public async void QuartzTest(int type,string jobkey,string group)
        {
            JobKey jobKey = new JobKey(jobkey, group);
            switch (type)
            {
                //添加任务
                case 1:
                    var trigger = TriggerBuilder.Create()
                            .WithDescription("触发器描述")
                            .WithIdentity("test")
                            
                            //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0/30 * * * ? *").WithMisfireHandlingInstructionDoNothing())
                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever().WithMisfireHandlingInstructionIgnoreMisfires())
                            .Build();
                     await QuartzUtil.Add(typeof(LibaryJob), jobKey, trigger);
                    break;
                //暂停任务
                case 2:
                    await QuartzUtil.Stop(jobKey);
                    break;
                //恢复任务
                case 3:
                    await QuartzUtil.Resume(jobKey);
                    break;
                case 4:
                    await QuartzUtil.Close(jobKey);
                    break;
                default:
                    Console.WriteLine("任务失败");
                    break;
            }
        }

    }
}
